<?php
/**
 * 大商创商品信息相关
 * auther sk
 * date 20201214
 */

namespace App\Http\Controllers\Dsc\Model;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Goods extends Model
{
    /**
     * 查看商品是否存在
     * auth 孙凯
     * date20201224
     * @param $PlatProductID
     * @return bool
     */
    public static function getproductModel($PlatProductID)
    {
        return DB::connection('mysql2')->table('goods')->where(['goods_sn' => $PlatProductID])->exists();

    }

    /**
     * 同步商品库存数量
     * auth 孙凯
     * date 20201224
     * @param $PlatProductID
     * @param $Quantity
     * @return int
     */
    public static function syncstockModel($PlatProductID, $Quantity)
    {
        $exits=mysql_cont('goods')->where(['goods_sn' => $PlatProductID,'goods_number' => $Quantity])->exists();
        if ($exits==true){
            return  1;
        }
        return DB::connection('mysql2')->table('goods')->where(['goods_sn' => $PlatProductID])->update(['goods_number' => $Quantity]);
    }

    /**
     * 吉客云获取商品信息
     * auther 孙凯
     * date 202012 25 12:53
     * @param $request
     * @return mixed
     */
    public static function getgoodsModel($request)
    {
        $getgoodsModel = DB::connection('mysql2')->table('goods');
        if (!empty($request['ProductId'])) {
            $getgoodsModel = $getgoodsModel->where('goods_id', '=', trim($request['ProductId']));
        }

        if (!empty($request['ProductName'])) {
            $getgoodsModel = $getgoodsModel->where('goods_name', 'like', '%' . $request['ProductName'] . '%');
        }

        if (!empty($request['Status'])) {
            if ($request['Status'] == 'JH_01') {
                $getgoodsModel = $getgoodsModel->where('is_on_sale', '=', 1);
            } elseif ($request['Status'] == 'JH_02') {
                $getgoodsModel = $getgoodsModel->where('is_on_sale', '=', 0);
            } else {
                $getgoodsModel = $getgoodsModel->where('is_on_sale', '<>', 500);
            }
        }

        $PageIndex = $request["PageIndex"] == 0 ? 0 : $request["PageIndex"] - 1;
        $select = [
            'goods_id as PlatProductID',
            'goods_sn as OuterID',
            'goods_name as name',
            'shop_price as price',
            'goods_number as num',
            'goods_img as pictureurl',
        ];
        $getgoodsModel = $getgoodsModel->select($select);
        $getgoodsModel = $getgoodsModel->orderByDesc('goods_id');
        $totalcount = $getgoodsModel->get()->count();
        $totalcount = $totalcount > 0 ? $totalcount : 1;
        $res = $getgoodsModel/*->offset($PageIndex)->limit($request['PageSize'])*/->get()->toArray();
        $res = objectToArray($res);
//        dd($res);
        $select_data = ['p.product_sn as skuOuterID',
            'p.product_number as skuQuantity',
            'p.product_price as skuprice',
            'ga.attr_value as skuname',
            'ga.goods_attr_id'];
        foreach ($res as $k => $v) {
            $result = DB::connection('mysql2')->table('products as p')
                ->join('goods_attr as ga', 'p.goods_attr', '=', 'ga.goods_attr_id')
                ->where(['p.goods_id' => $v['PlatProductID']])
                ->select($select_data)->get();
            $result = objectToArray($result);
            $array = [];
            foreach ($result as $item) {
                $value['SkuID'] = $v['PlatProductID'];
                $value['skuOuterID'] = $item['skuOuterID'];
                $value['skuprice'] = $item['skuprice'];
                $value['skuQuantity'] = $item['skuQuantity'];
                $value['skuname'] = $item['skuname'];
                $value['skuprice'] = $item['skuprice'];
                $array[] = $value;
            }
            $res[$k]['skus'] = $array;
        }
        $ressult['code'] = 10000;
        $ressult['message'] = "SUCCESS";
        $ressult['totalcount'] = $totalcount;
        $ressult['goodslist'] = $res;
        return $ressult;
    }

}